package top.datacluster.basic.algorithm.leetcode.link;

public class LeetCode1607 {
    public int lastRemaining(int n, int m) {

        NodeCircle root = new NodeCircle(0);
        NodeCircle head = root;
        for (int i = 1; i < n; i++){
            root.next = new NodeCircle(i);
            root = root.next;
        }
        root.next = head;
        int index = n;
        while (index > 1){
            int i = 0;
            while (i < m - 1){
                root = root.next;
                i++;
            }
            NodeCircle temp = root.next;
            root.next = root.next.next;
            temp.next = null;
            index--;
        }
        return root.val;

    }

    public static void main(String[] args) {
        new LeetCode1607().lastRemaining(70866,116922);
    }
}

class NodeCircle{
    int val;
    NodeCircle next;

    public NodeCircle(int val) {
        this.val = val;
    }
}
